/**
 * 
 */
package it.fuesi.myseries.api;

import it.fuesi.myseries.api.ApiClient.HTTPMethod;
import it.fuesi.myseries.model.MySeriesConstant;

import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.os.Bundle;

/**
 * @author fuesi
 *
 */
public abstract class AbstractApiTask {

	
	private Bundle currentBundle;
	private Context currentContext;
	
	
	public Bundle getCurrentBundle() {
		return currentBundle;
	}

	public Context getCurrentContext() {
		return currentContext;
	}

	protected void setContext(Bundle inputBundle, Context context){
		this.currentBundle = inputBundle;
		this.currentContext = context;
	}
	
	protected abstract Map<String,String> getApiParams();
	
	
	protected Bundle processResults(JSONObject response) throws JSONException{
		final Bundle results = new Bundle();
		
		try{
			results.putBoolean(MySeriesConstant.SUCCESS.toString(), response.getBoolean("success"));
			results.putString(MySeriesConstant.ERROR.toString(), response.has("error") ? response.getString("error") : "");
		}catch(Exception e){
			results.putBoolean(MySeriesConstant.SUCCESS.toString(), false);
			results.putString(MySeriesConstant.ERROR.toString(), e.getMessage());
		}
		
		
		return results;
	}
	
}
